﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace PersonaLibrary.Config
{
	internal sealed class OrderConfig
	{
		private Dictionary<string, List<string>> _orders = new Dictionary<string, List<string>>();
		private static OrderConfig _instance;

		private OrderConfig()
		{
			XmlDocument document = new XmlDocument();
			document.Load("Config\\OrderList.xml");
			XmlNodeList types = document.SelectNodes("root/type");
			foreach (XmlNode type in types)
			{
				if (_orders.ContainsKey(type.Attributes["name"].Value))
					throw new Exception(string.Format("Order config containse more than one {0} type", type.Attributes["name"].Value));

				List<string> items = new List<string>(type.ChildNodes.Count);

				foreach (XmlNode childNode in type.ChildNodes)
					items.Add(childNode.FirstChild.Value);

				_orders.Add(type.Attributes["name"].Value, items);
			}
		}

		public static OrderConfig Instance
		{
			get
			{
				if(_instance == null)
					_instance = new OrderConfig();
				return _instance;
			}
		}

		public List<string> GetOrders(string typeName)
		{
			List<string> result = null;
			_orders.TryGetValue(typeName, out result);
			return result;
		}
	}
}
